var fp;
var ds,grid;
var win,winfp;

var buildForm = function(){

	fp = new Ext.form.FormPanel({
		labelAlign : 'top',
		buttonAlign : 'center',
		bodyStyle : 'padding:5px 0px 0px 30px ',
		frame:false,
		width:175,
		autoWidth:true,
		autoHeight:true,
		defaults:{
			width:150
		},
		defaultType:'textfield',
		laypout:'form',
		items : [{
					name : 'name',
					fieldLabel : 'name'
				}],
		buttons : [{
					text : '查询',
					type : 'button',
					handler : queryRecord
				}, {
					text : '清空',
					type : 'button',
					handler : function() {
						fp.form.reset();
					}
				}]
	});
};

var buildGrid = function(){

    ds = new Ext.data.JsonStore({
        url:'a0000!list.action',
        root:'items',
        totalProperty:'total',
        idProperty:'id',
        fields:['id','hr01_00_001','huji_detail_address','hr02_01_001_01','hr02_02_001','xian_detail_address','familyId']
    });
	
    grid = new Ext.grid.EditorGridPanel({
        store:ds,
        columns:[
            new Ext.grid.RowNumberer(),
            <#list columns as item>
            	<#if item.columnType=='Date'>
            {header:'${item.propertyName}',width:200,sortable:true,dataIndex:'${item.propertyName}',renderer:Ext.util.Format.dateRenderer('Y-m-d')}<#if item.item_has_next>,</#if>
            	<#else>
            {header:'${item.propertyName}',width:200,sortable:true,dataIndex:'${item.propertyName}'}<#if item.item_has_next>,</#if>
            	</#if>
			</#list>
        ],
        renderTo:'grid',
        border:true,
        selModel:new Ext.grid.RowSelectionModel(),
        tbar : [{
			text:'create',
			iconCls:'admin-tool-add',
			handler:new${ModelName}
		}, '-', {
			text:'edit',
			iconCls:'admin-tool-edit',
			handler:edit${ModelName}
		}, '-', {
			text:'删除个人档案',
			iconCls:'admin-tool-delete',
			handler:destroy${ModelName}
		}],
        bbar : new Ext.PagingToolbar({
			store : ds,
			displayInfo : true
		})
    });
    
    ds.load();
};
var buildWin = function(){
    winfp = new Ext.form.FormPanel({
        labelWidth:60,
        labelAlign:'right',
        bodyStyle:'padding:10px',
        items:[{
            layout:'column',
            border:false,
            defaults:{border:false},
            items:[{
                columnWidth:.5,
                layout:'form',
                defaultType:'textfield',
                defaults:{width:180},
                items:[{
                    fieldLabel:'name',
                    name:'name',
                    allowBlank:false
                }
               ]
            }]
        }]
    });
    
    win = new Ext.Window({
        el:'win',
        width:600,
        height:385,
        closeAction:'hide',
        layout:'fit',
        modal:true,
        items:winfp,
        buttons:[{
            text:'保存',
            handler:save${ModelName}
        },{
            text:'取消',
            handler:function(){win.hide();}
        }]
    });
    
};
var buildLayout = function(){
    new Ext.Viewport({
        layout:'border',
        defaults:{border:false},
        items:[{
            region:'north',
            contentEl:'north-div',
            title:'list'
        },{
            region:'west',
            width:200,
            split:true,
            autoScroll:true,
            items:fp
        },{
            region:'center',
            layout:'fit',
            items:grid
        }]
    });
};

var init = function(){
    buildForm();
    buildGrid();
  	buildLayout();
};

Ext.onReady(init);

var queryRecord = function(){
	var params = {};
	params['name'] = fp.form.findField('name').getValue();
	ds.load({params : params});
};

var new${ModelName} = function(){
	win.setTitle('新建${ModelName}资料');
	win.show();
	fp.form.reset();
	fp.url = '${modelName}!save.action';
};

var edit${ModelName} = function(){
	var rec = grid.getSelectionModel().getSelected();
    if (rec){
        win.setTitle('修改${ModelName}资料');
        win.show();
        fp.form.reset();
        fp.form.loadRecord(rec);
        fp.url = '${modelName}!save.action?id='+rec.id;
    }
};

var destroy${ModelName} = function(){
	var rows = grid.getSelectionModel().getSelections();
	
	if (rows.length == 0){
		Ext.Msg.alert("提示", "您还没有选择任何记录！");
		return;
	}
	var ids = [];
	if (rows.length > 0){
		for (var i = 0; i < rows.length; i++) {
			ids.push(rows[i].data['id']);
		}
	}
	
	Ext.MessageBox.confirm("删除确认", "是否真的删除选定的资料?", function(btn){
		if(btn=='yes'){
			Ext.Ajax.request({
				url:'${modelName}!delete.action',
				method:'POST',
				params:{'ids':ids},
				success:function(response){
					var data = Ext.util.JSON.decode(response.responseText);
					ds.reload();
				    Ext.MessageBox.alert('警告','删除记录完成。');
				}
			});
		}
	});
};